﻿using System;
using System.Collections.Generic;
using System.Timers;

namespace TheSnake
{
    class TheSnakeMain
    {
        public const int WorldRows = 20;
        public const int WorldCols = 70;
        public int sleep = 200;
        static Timer timer; // From System.Timers
        public static MovingObsticle[] movingObsticles = new MovingObsticle[3];
        public static Engine gameEngine;

        public static void Initializer(Engine engine)
        {
            int startRow = 0;
            int startCol = 0;
            int endCol = WorldCols - 1;
            int endRow = WorldRows - 1;

            for (int i = startCol; i < endCol; i++)
            {
                Block currBlock = new Block(new MatrixCoords(startRow, i));

                engine.AddObject(currBlock);
            }
            for (int i = startCol; i < endCol; i++)
            {
                Block currBlock = new Block(new MatrixCoords(i, startRow));

                engine.AddObject(currBlock);
            }
            for (int i = startCol; i < endCol; i++)
            {
                Block currBlock = new Block(new MatrixCoords(i, endCol));

                engine.AddObject(currBlock);
            }
            for (int i = startCol; i < endCol; i++)
            {
                Block currBlock = new Block(new MatrixCoords(endRow, i));
                engine.AddObject(currBlock);
            }
            for (int i = 54; i < endCol; i++)
            {
                Block currBlock = new Block(new MatrixCoords(startRow + 3, i));

                engine.AddObject(currBlock);
            }
            for (int i = startRow; i < startRow + 3; i++)
            {
                Block currBlock = new Block(new MatrixCoords(i, 54));

                engine.AddObject(currBlock);
            }
            Random randGen = new Random();

            for (int i = 0; i < 11; i++)
            {
                Block hurdle = new Block(new MatrixCoords(randGen.Next(4, WorldRows - 1), randGen.Next(0, 53)));
                engine.AddObject(hurdle);
            }
            Food food = new Food(new MatrixCoords(randGen.Next(0, WorldRows - 1), randGen.Next(0, WorldCols - 1)));
            engine.AddObject(food);

            Bonus bonus = new Bonus(new MatrixCoords(randGen.Next(4, WorldRows - 1), randGen.Next(0, 53)));
            engine.AddObject(bonus);

            engine.AddObject(Snake.Instance);

            Status initial = new Status(new MatrixCoords(WorldRows / 2 + 1, WorldCols / 2 + 1), 5, 0);
            timer = new Timer(10000); // Set up the timer for 10 seconds
            timer.Elapsed += new ElapsedEventHandler(TimeOut);
            timer.Enabled = true; // Enable it
        }

        static void TimeOut(object sender, ElapsedEventArgs e)
        {
            MovingObsticle obsticle1 = new MovingObsticle(new MatrixCoords(6, 0), new MatrixCoords(0, 1));
            MovingObsticle obsticle2 = new MovingObsticle(new MatrixCoords(17, WorldCols - 1), new MatrixCoords(0, -1));
            MovingObsticle obsticle3 = new MovingObsticle(new MatrixCoords(WorldCols - 15, WorldRows - 1), new MatrixCoords(-1, 0));
            movingObsticles[0] = obsticle1;
            movingObsticles[1] = obsticle2;
            movingObsticles[2] = obsticle3;
            for (int i = 0; i < movingObsticles.Length; i++)
            {
                gameEngine.AddObject(movingObsticles[i]);
            }
        }

        public static void Main(string[] args)
        {
            Console.BufferHeight = Console.WindowHeight;
            Console.BufferWidth = Console.WindowWidth;
            Console.CursorVisible = false;
            IRenderer renderer = new ConsoleRenderer(WorldRows, WorldCols);
            IUserInterface keyboard = new KeyboardInterface();

            gameEngine = new Engine(renderer, keyboard, 200);

            keyboard.OnLeftPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerSnakeLeft();
            };

            keyboard.OnRightPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerSnakeRight();
            };

            keyboard.OnUpPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerSnakeUp();
            };

            keyboard.OnDownPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerSnakeDown();
            };

            Initializer(gameEngine);

            gameEngine.Run();
        }
    }
}